package com.bstek.weixin.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bstek.weixin.entity.Department;
import com.bstek.weixin.entity.WeixinUser;
import com.bstek.weixin.service.IUserListService;
import com.bstek.weixin.utils.BodyUtil;
import com.bstek.weixin.wxInterface.UserListInterFace;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @Description TEDO
 * @Author snail
 * @Date 2019/1/7
 * @Version 1.0
 */
@Service(UserListImpl.BEAN_ID)
public class UserListImpl implements IUserListService {

    public static final String BEAN_ID="msi.weix.userListImpl";

    @Override
    public boolean createDepartment(Department dept) throws Exception {
        //1、组装部门json
        String content= BodyUtil.departmentBody(dept); //
        if(content!=null){
            //2、调用微信接口
            JSONObject resultJSON=UserListInterFace.createDept(content);
           if(resultJSON!=null){
               String megCode=resultJSON.getString("errcode");
               if("0".equals(megCode)){
                  return true;
               }else{
                   throw new Exception("创建微信部门失败："+resultJSON.getString("errmsg"));
               }
           }else{
             throw new Exception("创建部门微信接口返回null");
           }
        }else{
            throw new NullPointerException("创建部门信息不能为null");
        }
    }

    @Override
    public boolean updateDepartment(Department dept) throws Exception {
        //1、组装部门json
        String content= BodyUtil.departmentBody(dept); //
        if(content!=null){
            //2、调用微信接口
            JSONObject resultJSON=UserListInterFace.updateDept(content);
            if(resultJSON!=null){
                String megCode=resultJSON.getString("errcode");
                if("0".equals(megCode)){
                    return true;
                }else{
                    throw new Exception("修改微信部门失败："+resultJSON.getString("errmsg"));
                }
            }else{
                throw new Exception("修改部门微信接口返回null");
            }
        }else{
            throw new NullPointerException("修改部门信息不能为null");
        }
    }

    @Override
    public boolean deleteDepartment(String deptId) throws Exception {
        if(StringUtils.hasText(deptId)){
            //调用微信接口
            JSONObject resultJSON=UserListInterFace.deleteDept(deptId);
            if(resultJSON!=null){
                String megCode=resultJSON.getString("errcode");
                if("0".equals(megCode)){
                    return true;
                }else{
                    throw new Exception("删除微信部：门失败"+resultJSON.getString("errmsg"));
                }
            }else{
                throw new Exception("删除部门微信接口返回null");
            }
        }else{
            throw new NullPointerException("部门ID不能为null");
        }
    }

    @Override
    public boolean createUser(WeixinUser user) throws Exception{
        //1、组装用户json
        String content= BodyUtil.userBody(user); //
        if(content!=null){
            //2、调用微信接口
            JSONObject resultJSON=UserListInterFace.createUser(content);
            if(resultJSON!=null){
                String megCode=resultJSON.getString("errcode");
                if("0".equals(megCode)){
                    return true;
                }else{
                    throw new Exception("创建微信用户失败："+resultJSON.getString("errmsg"));
                }
            }else{
                throw new Exception("创建微信用户接口返回null");
            }
        }else{
            throw new NullPointerException("创建用户信息不能为null");
        }
    }

    @Override
    public boolean updateUser(WeixinUser user) throws Exception{
        //1、组装部门json
        String content= BodyUtil.userBody(user); //
        if(content!=null){
            //2、调用微信接口
            JSONObject resultJSON=UserListInterFace.updateUser(content);
            if(resultJSON!=null){
                String megCode=resultJSON.getString("errcode");
                if("0".equals(megCode)){
                    return true;
                }else{
                    throw new Exception("修改用户失败："+resultJSON.getString("errmsg"));
                }
            }else{
                throw new Exception("修改用户微信接口返回null");
            }
        }else{
            throw new NullPointerException("修改用户信息不能为null");
        }
    }

    @Override
    public boolean deleteUser(String userId) throws Exception{
        if(StringUtils.hasText(userId)){
            //调用微信接口
            JSONObject resultJSON=UserListInterFace.deleteUser(userId);
            if(resultJSON!=null){
                String megCode=resultJSON.getString("errcode");
                if("0".equals(megCode)){
                    return true;
                }else{
                    throw new Exception("删除微信用户失败："+resultJSON.getString("errmsg"));
                }
            }else{
                throw new Exception("删除用户微信接口返回null");
            }
        }else{
            throw new NullPointerException("用户ID不能为null");
        }
    }


}
